package com.marathon.ershor.textrecognizer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by ershor on 2017/8/14.
 */

public class RecResultView extends View {

    private Paint mPaint;
    private Point mCenterPoint;
    private CopyOnWriteArrayList<RecObject> mArrayObject;

    public class RecObject {
        public String content;
        public String label;
        public String des;
        public Rect rect;
        public boolean bActivate;
        public RecObject() {
            content = "";
            label = "";
            des = "";
            rect = new Rect(0,0,0,0);
            bActivate = false;
        }
    }

    public void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mArrayObject = new CopyOnWriteArrayList<>();
    }

    public RecResultView(Context context) {
        super(context);
        init();
    }

    public RecResultView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RecResultView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setJson(String json, int iWidth, int iHeight) {
        if (json.length() <= 2 ) {
//            playTestCase();
            return;
        }

        try {
            mArrayObject.clear();
            JSONArray root = new JSONArray(json);
            float xRatio = mCenterPoint.x * 2 / (float) iWidth;
            float yRatio = mCenterPoint.y * 2 / (float) iHeight;
            for (int i = 0; i < root.length(); i++) {
                JSONObject one = (JSONObject) root.get(i);
                RecObject obj = new RecObject();
                if (one.has("content")) {
                    obj.content = one.getString("content");
                }
                if (one.has("label")) {
                    obj.label = one.getString("label");
                }
                if (one.has("des")) {
                    obj.des = one.getString("des");
                }
                JSONObject topLeft = (JSONObject) one.get("topleft");
                JSONObject bottomRight = (JSONObject) one.get("bottomright");

                obj.rect.left = (int)(topLeft.getInt("x") * xRatio);
                obj.rect.top = (int)(topLeft.getInt("y") * yRatio);
                obj.rect.right = (int)(bottomRight.getInt("x") * xRatio);
                obj.rect.bottom = (int)(bottomRight.getInt("y") * yRatio);
                if (one.has("activate")) {
                    obj.bActivate = one.getBoolean("activate");
                }

                if (obj.bActivate) {
                    String activate_des = TextUtils.isEmpty(obj.des) ? "" : obj.des;
                    TTSWrapper.getInstance(getContext()).play(activate_des);
                }
                mArrayObject.add(obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        postInvalidate();
    }

    private boolean bHasPlayTestCase = false;

    private void playTestCase() {
        if (bHasPlayTestCase)
            return;
        bHasPlayTestCase = true;
        mArrayObject.clear();
        {
            RecObject obj = new RecObject();
            obj.content = "美女";
            obj.label = "好一个妖艳贱货";
            obj.rect = new Rect();
            obj.rect.left = mCenterPoint.x - 60;
            obj.rect.top = mCenterPoint.y - 100;
            obj.rect.right = obj.rect.left + 150;
            obj.rect.bottom = obj.rect.top + 250;

            TTSWrapper.getInstance(getContext()).play(obj.label);
            mArrayObject.add(obj);
        }
        {
            RecObject obj = new RecObject();
            obj.content = "鹤顶红";
            obj.label = "天下第一奇毒";
            obj.rect = new Rect();
            obj.rect.left = 10;
            obj.rect.top = 20;
            obj.rect.right = obj.rect.left + 150;
            obj.rect.bottom = obj.rect.top + 100;
            mArrayObject.add(obj);
        }
        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 画矩形
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(5);
        mPaint.setAlpha(255);
        for (int i = 0; i < mArrayObject.size(); i++) {
            RecObject one = mArrayObject.get(i);
            if (!one.bActivate
                    && one.label != null
                    && one.label.compareTo("文字") == 0)
                continue; // do not draw rect for label:text
            if (one.bActivate) {
                mPaint.setColor(Color.RED);
            } else {
                mPaint.setColor(Color.GREEN);
            }
            mPaint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(one.rect, mPaint);
        }

        // 中心点
        int width = this.getWidth();
        int height = this.getHeight();
        mCenterPoint = new Point(width / 2, height / 2);
        mPaint.setColor(Color.RED);
        mPaint.setAlpha(255);
        mPaint.setStrokeWidth(10);
        mPaint.setStyle(Paint.Style.FILL);
        int crossLineLength = Math.min(mCenterPoint.x, mCenterPoint.y) / 10;
        canvas.drawLine(mCenterPoint.x, mCenterPoint.y - crossLineLength,
                mCenterPoint.x, mCenterPoint.y + crossLineLength, mPaint);
        canvas.drawLine(mCenterPoint.x - crossLineLength, mCenterPoint.y,
                mCenterPoint.x + crossLineLength, mCenterPoint.y, mPaint);

        // 绘文字
        mPaint.setColor(Color.YELLOW);
        mPaint.setAlpha(255);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(3);
        mPaint.setTextSize(40);
        for (int i = 0; i < mArrayObject.size(); i++) {
            RecObject one = mArrayObject.get(i);
            // content 写正中间
            String s = one.content;
            if (TextUtils.isEmpty(s)) {
                s = one.label;
            }
            if (!TextUtils.isEmpty(s)) {
                mPaint.setTextAlign(Paint.Align.CENTER);
                canvas.drawText(s, one.rect.centerX(), one.rect.centerY(), mPaint);
            }
        }
    }
}
